package com.br.planningrun.repositorio;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.br.planningrun.entidade.Config;

public class RepositorioConfig {
	// Atributos
	private Context context;
	private SQLiteDatabase db2 = null;
	private SQLiteStatement stmt = null;
	private Cursor c = null;
	
	private static final String NOME_TABELA = "tb_config";
	private static final String BANCO = "planningrun";
	
	// Construtor
	public RepositorioConfig(Context context){
		this.context = context;
	}	
	
	// Inserir objeto
    public void inserir(Config config){        
        try {
        	db2 = context.openOrCreateDatabase(BANCO, 0, null);

        	stmt = db2.compileStatement("INSERT INTO " 
    				+ NOME_TABELA + " (codigo," 
    				+ "ip, porta,contexto,versao) VALUES (?, ?, ?, ?, ?)");

    		stmt.bindLong(1, 1);
    		stmt.bindString(2, config.getIpServidor());
    		stmt.bindLong(3, config.getPortaServidor());
    		stmt.bindString(4, config.getContextoServidor());
    		stmt.bindString(5, config.getVersao());
    		
    		stmt.execute();
        } finally {
        	if (stmt != null) {
        		stmt.close();
        	}
        	
        	if (c != null) {
        		c.close();
        	}
        	
        	if (db2 != null) {
        		db2.close();
        	}
        }
    }
	    

    // Atualizacao dos dados do servidor
    public void atualizarInformacoesServidor(String ip, int porta, String contexto){
    	try {
    		db2 = context.openOrCreateDatabase(BANCO, 0, null);	        
        	
        	stmt = db2.compileStatement("UPDATE " 
        			+ NOME_TABELA + " SET ip = ?, porta = ?, contexto = ? where codigo = ? ;");
    		
    		stmt.bindString(1, ip);
    		stmt.bindLong(2, porta);
    		stmt.bindString(3, contexto);
    		stmt.bindLong(5, 1);

    		stmt.execute();
    		
            db2.close();
    	} finally {
    		if (stmt != null) {
        		stmt.close();
        	}
        	
        	if (c != null) {
        		c.close();
        	}
        	
        	if (db2 != null) {
        		db2.close();
        	}
    	}
    }

    // Consulta do objeto
    public Config procurar(){        
        Config config = null;
        
        try {
        	db2 = context.openOrCreateDatabase(BANCO, 0, null);        
            c = db2.rawQuery("SELECT * FROM " + NOME_TABELA + " where codigo = 1 ", null);
                   
            while(c.moveToNext()){	        	
            	config = new Config();        	
            	config.setIpServidor(c.getString(c.getColumnIndex("ip")));
            	config.setPortaServidor(Integer.parseInt(c.getString(c.getColumnIndex("porta"))));
            	config.setContextoServidor(c.getString(c.getColumnIndex("contexto")));
            	config.setVersao(c.getString(c.getColumnIndex("versao")));
            }
    	} finally {
    		if (stmt != null) {
        		stmt.close();
        	}
        	
        	if (c != null) {
        		c.close();
        	}
        	
        	if (db2 != null) {
        		db2.close();
        	}
    	}
        
        return config;
    }
    
	// Criacao da tabela
	public void truncate() {
		try {
			db2 = context.openOrCreateDatabase(BANCO, 0, null);
			// Exclusao da tabela se existir
			db2.execSQL("DROP TABLE IF EXISTS " + NOME_TABELA);
			// Criacao da tabela
			db2.execSQL("CREATE TABLE IF NOT EXISTS " + NOME_TABELA + "( codigo INTEGER NOT NULL PRIMARY KEY, " +
					"ip varchar2(255) , " +
					"porta INTEGER," +
					"contexto varchar2(255) ," +
					"versao varchar2(255) );");
    	} finally {
    		if (stmt != null) {
        		stmt.close();
        	}
        	
        	if (c != null) {
        		c.close();
        	}
        	
        	if (db2 != null) {
        		db2.close();
        	}
    	}
	}

}
